package com.android.bookgarden.ui;

import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.bookgarden.R;
import com.android.bookgarden.apis.Apis;
import com.android.bookgarden.base.BaseActivity;
import com.android.bookgarden.bean.DataBean;
import com.android.bookgarden.constract.RealtConstract;
import com.android.bookgarden.network.HttpType;
import com.android.bookgarden.persenter.BasePersenter;
import com.android.bookgarden.utli.LogUtil;
import com.android.bookgarden.utli.Utlis;
import com.android.bookgarden.views.LoadingDialog;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Create by zouqh
 * on 2018/11/24
 **/
public class FeedbackActivity extends BaseActivity implements View.OnClickListener, RealtConstract.View {
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @BindView(R.id.ritgh_text)
    TextView ritghText;
    @BindView(R.id.input)
    EditText input;
    @BindView(R.id.submit)
    TextView submit;
    private RealtConstract.Presenter presenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initView() {
        titleText.setText("意见或反馈");
        presenter = new BasePersenter(this, this);
        close.setOnClickListener(this);
        submit.setOnClickListener(this);
        submit.setEnabled(false);
        submit.setSelected(false);
        input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString() != null && !"".equals(s.toString())) {
                    submit.setEnabled(true);
                } else {
                    submit.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onRequestPermissions(String str, boolean z) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                finish();
                break;
            case R.id.submit:
                if (submit.isEnabled()) {
                    LoadingDialog.showMassge(this, "提交中...");
                    Map<String, String> map = new HashMap<>();

                    map.put("content", input.getText().toString());
                    presenter.loadDataHeader(map, "", Apis.FEEDBACK, 1001, Utlis.getHeadler(this), HttpType.POST);
                }
                break;
        }
    }

    @Override
    public void returnData(String data, int urlCode) {
        LogUtil.e("返回数据", data + "");
        if (TextUtils.isEmpty(data)) {
            return;
        }

        handler.obtainMessage(urlCode, data).sendToTarget();
    }

    @Override
    public void LoadError(String errorMsg, int urlCode) {
        showToast("提交失败");
        LoadingDialog.hideProgress();
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String data = (String) msg.obj;
            switch (msg.what) {
                case 1001:
                    try {
                        LoadingDialog.hideProgress();
                        DataBean bean = Utlis.getDataBean(data);
                        showToast(bean.getMessage());
                        if (bean.getCode() == 200) {
                            finish();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;

            }
        }
    };
}
